package com.jinxin.platform.cdacsfacetalk.controller;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacsfacetalk.common.Constant;
import com.jinxin.platform.cdacsfacetalk.rpc.AcsFaceTalkRpc;
import com.jinxin.platform.cdacsfacetalk.service.IAcsFaceTalkService;
import com.jinxin.platform.cdacsfacetalk.utils.FaceTalkRedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-05-14
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/unAuth")
@Slf4j
public class UnAuthFaceTalkController {
    @Autowired
    private IAcsFaceTalkService acsFaceTalkService;
    @Autowired
    private AcsFaceTalkRpc acsFaceTalkRpc;
    @Resource
    private FaceTalkRedisUtil redisUtil;

    @RequestMapping(value = "/weixin/validate", method = RequestMethod.GET)
    public ModelAndView handleWeiXinMessageRequestGet(
            HttpServletRequest request, HttpServletResponse response) {

        acsFaceTalkService.validateToken(request, response);

        return null;
    }

    @RequestMapping("/api/downdata/call.html")
    public Map<String,Object> faceAll(@RequestParam String controllersn, @RequestParam String roomId){
        return acsFaceTalkService.faceAll(controllersn,roomId);
    }

    @RequestMapping("/weixin/openDoor")
    public JsonResult openDoor(String serialNum, String productCode){
        return acsFaceTalkRpc.remoteOpenDoor(serialNum,productCode);
    }

    @RequestMapping("/facetalk/connected")
    public JsonResult connected(String roomId){
        if(StringUtils.isEmpty(roomId)){return null;}
        // 连接成功,将成功标识存入redis
        String key = Constant.FACE_TALK_CONNECTED_PREFIX + roomId;
        redisUtil.set(key,"success");
        return JsonResult.ok("success");
    }

}
